//import api from '../data/index';
import * as types from './types';
import api from '../data/api';

const state = {
    phone:'',
    // 用户登录状态
    loginStatus: JSON.parse(localStorage.getItem('loginStatus')) || false,
    // 用户登录信息
    userInfo: JSON.parse(localStorage.getItem('userInfo')) || {},

    userProfile: JSON.parse(localStorage.getItem('userProfile')) || {},

    contractStatus: JSON.parse(localStorage.getItem('contractStatus')) || {},

    faAllStatus: JSON.parse(localStorage.getItem('faAllStatus')) || {},

    contractSign: JSON.parse(localStorage.getItem('contractSign')) || {},

    contractPermit: JSON.parse(localStorage.getItem('contractPermit')) || {},

    warrantorInfo: JSON.parse(localStorage.getItem('warrantorInfo')) || {}
    // 用户数据信息
    // userData: []
};


const actions = {
    setUserInfo({ commit }, res) {
        localStorage.setItem('userInfo', JSON.stringify(res))
        localStorage.setItem('loginStatus', true);
        commit(types.SET_USER_INFO, res);
        commit(types.SET_LOGIN_STATUS, true);
    },


    setPhone({ commit }, res) {
        commit(types.SET_PHONE, res);
    },

    setUserProfile({ commit }, res) {
        localStorage.setItem('userProfile', JSON.stringify(res))
        commit(types.SET_USER_PROFILE, res);
    },


    /* setContractStatus({ commit }, res){
        localStorage.setItem('contractStatus', JSON.stringify(res))
        commit(types.SET_CONTRACT_STATUS, res);
    }, */

    async getContractStatus({ commit }){

        var res = await api.user.get_contract_status();
        localStorage.setItem('contractStatus', JSON.stringify(res))
        commit(types.SET_CONTRACT_STATUS, res);
    },

    async setContractPermit({ commit }){
        var res = await api.user.investor_contract_permit();
        localStorage.setItem('contractPermit', JSON.stringify(res))
        commit(types.SET_CONTRACT_PERMIT, res);
    },



    async setContractSign({ commit }){
        var res = await api.user.investor_sign_contract();
        localStorage.setItem('contractSign', JSON.stringify(res))
        commit(types.SET_CONTRACT_SIGN, res);
    },

    async getFadAllStatus({ commit }){
        var res = await api.user.get_contract_all_status();

        localStorage.removeItem('faAllStatus')
        localStorage.setItem('faAllStatus', JSON.stringify(res))
        commit(types.SET_C_ALL_STATUS, res);
    },

    async getWarrantorInfo({ commit }){
        var res = await api.user.warrantor_info();

        localStorage.removeItem('warrantorInfo');
        localStorage.setItem('warrantorInfo', JSON.stringify(res.data))
        commit(types.SET_WARRANTOR_INFO, res.data);
    },



    /* setCAllStatus({ commit }, res) {
        localStorage.setItem('cAllStatus', JSON.stringify(res))
        commit(types.SET_C_ALL_STATUS, res);
    }, */

    /* setSignOut({ commit }) {

        api.user.loginOut().then(function (res) {
            localStorage.removeItem('loginStatus')
            localStorage.removeItem('userInfo')
            commit(types.SET_LOGIN_STATUS, false)
            commit(types.SET_USER_INFO, {})
        });
    }, */


    setLoginStatus({ commit }, status) {
        commit(types.SET_LOGIN_STATUS, status)
    },


}

const getters = {
    phone: state => state.phone,
    loginStatus: state => state.loginStatus,
    userInfo: state => state.userInfo,
    userProfile: state => state.userProfile,
    contractStatus: state => state.contractStatus,
    faAllStatus: state => state.faAllStatus,
    contractPermit: state => state.contractPermit,
    contractSign: state => state.contractSign,
    warrantorInfo: state => state.warrantorInfo

}


const mutations = {

    [types.SET_PHONE](state, status) {
        state.phone = status;
    },

    [types.SET_LOGIN_STATUS](state, status) {
        state.loginStatus = status;
    },

    [types.SET_USER_INFO](state, res) {
        state.userInfo = res;
    },

    [types.SET_USER_PROFILE](state, res) {
        state.userProfile = res;
    },

    [types.SET_CONTRACT_STATUS](state, res){
        state.contractStatus = res;
    },

    [types.SET_C_ALL_STATUS](state, res) {
        state.faAllStatus = res;
    },


    [types.SET_CONTRACT_PERMIT](state, res) {
        state.contractPermit = res;
    },

    [types.SET_CONTRACT_SIGN](state, res) {
        state.contractSign = res;
    },

    [types.SET_WARRANTOR_INFO](state, res) {
        state.warrantorInfo = res;
    },

}

export default {
    state,
    actions,
    getters,
    mutations
}
